<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
            <a-col :md="6" :sm="8">
                <a-form-item label="年度">
                    <a-date-picker
                        style="width:100%"
                        :open='yearShowOne'
                        @openChange="openChangeOne"
                        @panelChange="panelChangeOne"
                        mode="year"
                        format="YYYY"
                        type="date"
                        :default-value="moment(new Date(), 'YYYY')"
                        v-model="queryParam.shijian"
                        suffix-icon=" "
                        placeholder="年份"
                    />
                </a-form-item>
            </a-col>
            <!-- <a-col :md="6" :sm="8">
                <a-form-item label="成果类别">
                    <a-select v-model="queryParam.flid" placeholder="请选择成果类别">
                        <a-select-option v-for="(item, index) in cxjSbList" :key="index" :value="item.fl_bh">{{item.fl_mc}}</a-select-option>
                    </a-select>
                </a-form-item>
            </a-col>
            <a-col :md="6" :sm="8">
                <a-form-item label="大奖分组">
                    <a-select v-model="queryParam.fl_mc" placeholder="请选择分组名称">
                      <a-select-option v-for="item in fsfzlist" :key="item.fl_id" :value="item.fl_id">
                          {{item.fl_mc}}
                      </a-select-option>
                  </a-select>
                </a-form-item>
            </a-col> -->
            <!-- <a-col :md="6" :sm="8">
                <a-form-item label="奖项">
                    <a-select v-model="queryParam.jxdj" placeholder="请选择奖项等级">
                      <a-select-option value="1">一等奖</a-select-option>
                      <a-select-option value="2">二等奖</a-select-option>
                    </a-select>
                </a-form-item>
            </a-col> -->
            <!-- <a-col :md="6" :sm="8">
                <a-form-item label="关键词">
                    <a-input placeholder="请填写成果编号或成果名称" v-model="queryParam.gjc"></a-input>
                </a-form-item>
            </a-col> -->
            <a-col :sm="8">
                <a-form-item label="">
                    <a-button style="margin-right:10px;" type="primary" @click="searchQuery" icon="search">查询</a-button>
                    <a-button style="margin-right:10px;" type="primary" @click="searchReset" icon="reload">重置</a-button>
                    <!-- <a-button style="margin-right:10px;" type="primary" @click="exportxmlist">导出</a-button> -->
                    <a-button style="margin-right:10px;" type="primary" @click="importxmlist">导入</a-button>
                    <input type="file" style="display:none;" class="pointer zxpd" id="zxpd" accept=".xls,.xlsx" @change="handleUploadzxpd"/>
                </a-form-item>
            </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <!-- <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('大奖评审分组项目表')">导出</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload> -->
      <!-- 高级查询区域 -->
      <!-- <j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown> -->
    </div>

    <!-- table区域-begin -->
    <div>
      <!-- <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div> -->

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <span slot="shifouhuojiang" slot-scope="text, record">
          <a-switch v-model:checked="record.is_awarded == 'Y' ? true : false" checked-children="是" un-checked-children="否" @change="changeStatus($event, record)"></a-switch>
        </span>

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>

          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>

    <cxj-djps-xm-modal ref="modalForm" @ok="modalFormOk"></cxj-djps-xm-modal>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import CxjDjpsXmModal from './modules/CxjDjpsXmModal'
  import moment from 'moment'
  import { filterObj } from '@/utils/util';
  import {
      getAction,
      cxjdjps_xmList_isAwarded,
      cxjdjps_xmList_import
  } from "@/api/manage"
  import {
    sysCxjFlCxjsbList
} from "@/api/api"
  export default {
    name: 'CxjDjpsXmList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      CxjDjpsXmModal
    },
    data () {
      return {
        description: '特等奖评审分组项目表管理页面',
        // 表头
        columns: [
          {
            title: '序号',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'分组',
            align:"center",
            dataIndex: 'fl_mc'
          },
          {
            title:'答辩序号',
            align:"center",
            dataIndex: 'xm_xh'
          },
          {
            title:'年度',
            align:"center",
            dataIndex: 'shijian'
          },
          {
            title:'是否获奖',
            align:"center",
            dataIndex: 'shifouhuojiang',
            scopedSlots: { customRender: 'shifouhuojiang' }
          },
          // {
          //   title:'成果类别',
          //   align:"center",
          //   dataIndex: 'flId',
          //   customRender: function(id){
          //       return id == 11 ? '技术成果': id == 21?'管理成果':id == 31?'标准成果':id==41?'信息化成果':id=='51'?'专利成果':''
          //   },
          // },
          {
            title:'成果编号',
            align:"center",
            dataIndex: 'cxj_xmbh'
          },
          {
            title:'成果名称',
            align:"center",
            dataIndex: 'cxj_mc_zw'
          },
          {
            title:'申报单位',
            align:"center",
            dataIndex: 'sbdw_qymc'
          },
          {
            title:'所属集团',
            align:"center",
            dataIndex: 'jttjdw_mc'
          },
          {
            title:'联系人姓名',
            align:"center",
            dataIndex: 'lxr_xm'
          },
          {
            title:'手机',
            align:"center",
            dataIndex: 'lxr_sj'
          },
          {
            title:'邮箱',
            align:"center",
            dataIndex: 'lxr_yx'
          },
          // {
          //   title:'电话',
          //   align:"center",
          //   dataIndex: 'dh'
          // },
          // {
          //   title: '操作',
          //   dataIndex: 'action',
          //   align:"center",
          //   fixed:"right",
          //   width:147,
          //   scopedSlots: { customRender: 'action' }
          // }
        ],
        url: {
          list: "/cxj/cxjDjpsXm/cxjdjps_xmList",
          cxjdjps_fzlist: "/cxj/cxjDjpsXm/cxjdjps_getCgfl",
          delete: "/cxj/cxjDjpsXm/delete",
          deleteBatch: "/cxj/cxjDjpsXm/deleteBatch",
          exportXlsUrl: "/cxj/cxjDjpsXm/exportXls",
          importExcelUrl: "cxj/cxjDjpsXm/importExcel",

        },
        yearShowOne:false,
        shijian: moment(new Date()).format('YYYY'),
        dictOptions:{},
        superFieldList:[],
        cxjSbList:[],
        fsfzlist:[],
      }
    },
    created() {
      this.getSuperFieldList();
      this.getsysCxjFlCxjsbList();
      this.getcxjdjps_fzlist();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      moment,
      initDictConfig(){
      },
      changeStatus (val, row) {
        console.log(val, row)
        let that = this;
        let s = val === true ? 'Y' : 'N'
        cxjdjps_xmList_isAwarded({
          id: row.id,
          isAwarded: s
        }).then(res => {
          if (res.code == 200) {
            that.$message.success(res.message);
            this.loadData();
          } else {
            that.$message.error(res.message);
          }
        })
      },
      downfn(name, res) {
          const _res = res;
          let blob = new Blob([_res], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'});
          let downloadElement = document.createElement("a");
          let href = window.URL.createObjectURL(blob); //创建下载的链接
          downloadElement.href = href;
          downloadElement.download = name; //下载后文件名
          document.body.appendChild(downloadElement);
          downloadElement.click(); //点击下载
          document.body.removeChild(downloadElement); //下载完成移除元素
          window.URL.revokeObjectURL(href); //释放掉blob对象
      },
      importxmlist() {
        const choosefile = document.querySelector('#zxpd');
        choosefile.click();
      },
      handleUploadzxpd() {
        let that = this;
        const files = document.getElementById('zxpd').files[0];
        if (files) {
          let formData = new FormData();
          formData.append('file', files);
          cxjdjps_xmList_import(formData).then(res=>{
            if (res.code=='200') {
              document.getElementById('zxpd').value = null;
              that.$message.success('上传成功');
              that.loadData();
            } else {
              that.$message.error('上传失败');
            }
          }).catch(err=>{
              console.log(err)
          })
        }
      },
      /* 获取成果类别开始 */
      getsysCxjFlCxjsbList(){
          sysCxjFlCxjsbList(null).then(res=>{
              if (res.success) {
                  this.cxjSbList = res.result;
              }
          })
      },
      /* 获取成果类别结束 */
      getcxjdjps_fzlist() {
          getAction(this.url.cxjdjps_fzlist,{
            shijian: this.shijian
          }).then((res) => {
              if (res.success) {
                  this.fsfzlist = res.result.records || res.result;
              }
          })
      },
      handleSelectFz(value){
          this.flid = value;
          this.getcxjfs_fzlist(this.flid);
          // this.loadData(1);
      },
      /* 获取列表固定格式开始 */
      getQueryField() {
          //TODO 字段权限控制
          var str = "id,";
          this.columns.forEach(function (value) {
              str += "," + value.dataIndex;
          });
          return str;
      },
      // 获取分组项目
      loadData(arg) {
          if (!this.url.list) {
              this.$message.error("请设置url.list属性!")
              return
          }
          //加载数据 若传入参数1则加载第一页的内容
          if (arg === 1) {
              this.ipagination.current = 1;
          }
          var params = this.getQueryParams();//查询条件
          this.loading = true;
          // 分组id
          getAction(this.url.list, params).then((res) => {
              if (res.success) {
                  //update-begin---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
                  this.dataSource = res.result.records || res.result;
                  if (res.result.total) {
                      this.ipagination.total = res.result.total;
                  } else {
                      this.ipagination.total = 0;
                  }
                  //update-end---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
              }
              if (res.code === 510) {
                  this.$message.warning(res.message)
              }
              this.loading = false;
          })
      },
      onSelectChange(selectedRowKeys, selectionRows) {
          this.selectedRowKeys = selectedRowKeys;
          this.selectionRows = selectionRows;
      },
      onClearSelected() {
          this.selectedRowKeys = [];
          this.selectionRows = [];
      },
      modalFormOk() {
          // 新增/修改 成功时，重载列表
          this.loadData();
          //清空列表选中
          this.onClearSelected()
      },
      handleTableChange(pagination, filters, sorter) {
          //分页、排序、筛选变化时触发
          //TODO 筛选
          if (Object.keys(sorter).length > 0) {
              this.isorter.column = sorter.field;
              this.isorter.order = "ascend" == sorter.order ? "asc" : "desc"
          }
          this.ipagination = pagination;
          this.loadData();
      },

      getQueryParams() {
          //获取查询条件
          let sqp = {}
          if (this.superQueryParams) {
              sqp['superQueryParams'] = encodeURI(this.superQueryParams)
              sqp['superQueryMatchType'] = this.superQueryMatchType
          }
          this.queryParam.shijian = this.shijian;
          var param = Object.assign(sqp, this.queryParam, this.isorter, this.filters);
          param.field = this.getQueryField();
          param.pageNo = this.ipagination.current;
          param.pageSize = this.ipagination.pageSize;
          return filterObj(param);
      },
      handleAdd: function () {
          this.$refs.modalForm.add();
          this.$refs.modalForm.title = "新增";
          this.$refs.modalForm.disableSubmit = false;
      },
      handleEdit: function (record) {
          this.$refs.modalForm.edit(record);
          this.$refs.modalForm.title = "编辑";
          this.$refs.modalForm.disableSubmit = false;
      },
      searchQuery() {
          this.loadData(1);
      },
      searchReset() {
          this.queryParam = {};
          // this.flid = "";
          this.shijian = moment(new Date()).format('YYYY');
          this.queryParam.shijian = this.shijian;
          // this.getcxjfs_fzlist();
          this.loadData(1);
      },
      openChangeOne(status){
        //status是打开或关闭的状态
        if(status){
            this.yearShowOne =  true;
        }
      },
      // 得到年份选择器的值
      panelChangeOne(value){
        // params.shijian = value;
        this.shijian = moment(value).format('YYYY');
        this.queryParam.shijian = this.shijian;
        this.yearShowOne = false
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'string',value:'cxjCxjId',text:'成果ID',dictCode:''})
        fieldList.push({type:'string',value:'shijian',text:'年度',dictCode:''})
        fieldList.push({type:'string',value:'flId',text:'分组id',dictCode:''})
        fieldList.push({type:'string',value:'flMc',text:'分组名称',dictCode:''})
        fieldList.push({type:'int',value:'xmXh',text:'排序号',dictCode:''})
        fieldList.push({type:'string',value:'isValid',text:'是否有效',dictCode:''})
        fieldList.push({type:'string',value:'bz1',text:'备注1',dictCode:''})
        fieldList.push({type:'string',value:'bz2',text:'备注2',dictCode:''})
        this.superFieldList = fieldList
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>